import React, { Component } from 'react'

export default class GetDerivedStateFromProps extends Component {
    constructor(props) {
        super(props)
        this.state = {
            newCount: 0
        }
    }

    static getDerivedStateFromProps(props, prevState) {
        console.log(props, "---", prevState)
        // console.log("不能访问 this.state", this.state.newCount)
        // console.log("不能访问 this.props", this.props)
     return {
        bao: '1111'
     }
    }

    shouldComponentUpdate(a, b) {
        // console.log(a, 'shouldComponentUpdate--', b)
        // console.log("shouldComponentUpdate--", this.state)
        return true
    }
    
    componentDidUpdate(prevProps, prevState) {
        // console.log(prevProps,"componentDidUpdate", prevState)
        // console.log("this", this.state, this.props)
       
    }

    componentDidMount() {
        this.setState({
            newCount: this.props.count
        })
    }

    btnAdd() {
        // this.setState({
        //     newCount: ++this.state.newCount
        // }, () => {
        //     console.log("****", this.state.newCount)
        // })

        // this.setState((a) => {
        //     console.log(a)
        //     return {newCount: ++a.newCount}
        // }, () => {
        //     console.log("888", this.state.newCount)
        // })
    }

  render() {
    //   console.log("render", this.props)
    return (
      <div>
           {this.state.newCount} 子组件展示  {this.props.count}
           <button onClick={this.btnAdd.bind(this)} > 点击 newCount </button>
      </div>
    )
  }
}
